package com.lin.dao;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;
     static {
         InputStream inputStream = BaseDao.class.getResourceAsStream("/db.properties");
         Properties properties = new Properties();
         try {
             properties.load(inputStream);
             driver = properties.getProperty("driver");
             url = properties.getProperty("url");
             user = properties.getProperty("user");
             password = properties.getProperty("password");
         } catch (IOException e) {
             e.printStackTrace();
         }
     }

     //公共连接
    public static Connection getConnect() {
        Connection connection = null;

        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    //公共查询
    public static ResultSet execute(Connection connection,PreparedStatement preparedStatement, ResultSet resultSet, String sql,Object[] params){
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i+1,params[i]);
            }
            resultSet = preparedStatement.executeQuery();
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }
    public static int execute(Connection connection,PreparedStatement preparedStatement,String sql,Object[] params) throws Exception{
        int updateRows = 0;
        preparedStatement = connection.prepareStatement(sql);
        for(int i = 0; i < params.length; i++){
            preparedStatement.setObject(i+1, params[i]);
        }
        updateRows = preparedStatement.executeUpdate();
        return updateRows;
    }

    public static  boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
        boolean flag = true;
        if(resultSet != null){
            try {
                resultSet.close();
                resultSet = null;//GC����
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }
        if(preparedStatement != null){
            try {
                preparedStatement.close();
                preparedStatement = null;//GC����
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }
        if(connection != null){
            try {
                connection.close();
                connection = null;//GC����
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }

        return flag;
    }
}
